package com.seele.tigerwallet.ui.base;

import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.support.v4.view.MenuItemCompat;
import android.support.v7.widget.SearchView;
import android.support.v7.widget.Toolbar;
import android.text.Editable;
import android.text.InputFilter;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;


import com.seele.tigerwallet.R;
import com.seele.tigerwallet.ui.utils.StatusBarUtils;
import com.seele.tigerwallet.ui.views.RippleLayout;
import com.umeng.commonsdk.debug.W;

import retrofit2.http.PUT;

public abstract class ToolbarBaseActivity extends BaseActivity {

    private View mView;
    private LinearLayout mContainerLl;

    protected LinearLayout mBtnBack, mMiddleContainer, mRightContainer, mBaseLl;
    protected EditText mSearchEditext;

    private TextView mRightText;

    protected RelativeLayout tool_bar;
    protected ImageView mRightImg;
    private TextView mTitleTx;
    private Drawable drawable;
    private Drawable closeDrawable;
    protected ImageView mImgBack;


    protected abstract int getContainerViewId();

    @Override
    protected int getLayoutId() {
        return R.layout.activity_toolbar_base;
    }

    @Override
    protected void init() {
        initWidow();
        tool_bar = findViewById(R.id.tool_bar);
        mBaseLl = findViewById(R.id.base_ll);
        mContainerLl = findViewById(R.id.base_container);
        mBtnBack = findViewById(R.id.btn_back);
        mMiddleContainer = findViewById(R.id.middle_container);
        mImgBack = findViewById(R.id.img_back);
        mRightContainer = findViewById(R.id.right_container);
        mRightText = findViewById(R.id.right_text);
        mRightImg = findViewById(R.id.right_img);
        mTitleTx = findViewById(R.id.middle_title);
        int layoutId = getContainerViewId();
        mView = View.inflate(this, layoutId, null);


        ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        mView.setLayoutParams(layoutParams);
        addContainerView();
        initView();

        mBtnBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onBcak();
            }
        });
        mMiddleContainer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onMiddle();
            }
        });
        mRightContainer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onRight();
            }
        });
        mRightText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onRight();
            }
        });
        mRightImg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onRight();
            }
        });


//        RippleLayout.on(mBtnBack)
//                .rippleColor(getResources().getColor(R.color.light_grey))
//                .rippleDuration(250)
////                .rippleAlpha(0.2f)
//                .rippleHover(true)
//                .create();


    }

    public void setLeftImg(int resId) {
        mImgBack.setImageDrawable(getResources().getDrawable(resId));

    }

    public void setBackgroundColor(int resId) {
        mBaseLl.setBackgroundColor(getResources().getColor(resId));

    }

    protected abstract void initView();

    /**
     * 一个公共的Byid的接口
     *
     * @param id
     * @param <E>
     * @return
     */
    public final <E extends View> E $f(int id) {
        try {
            return (E) mView.findViewById(id);
        } catch (Exception ex) {
            Log.e("BaseFragment", "Could not cast View to concrete class.", ex);
            throw ex;
        }
    }

    protected void setVisibility(View view, int visibility) {
        if (null == view) {
            return;
        }
        view.setVisibility(visibility);

    }

    public void addContainerView() {
        if (null != mView) {
            mContainerLl.addView(mView);
        }
    }


    protected void onRightText() {
    }

    public void addMinddleView(View view) {
        if (null == view) {
            return;
        }
        mMiddleContainer.addView(view);
    }

    public void addRightView(View view) {
        if (null == view) {
            return;
        }
        mRightContainer.addView(view);
    }

    /**
     * 右边控件点击事件
     */
    private void onRight() {

        if (mRightText.getVisibility() == View.VISIBLE) {
            onRightText();
        } else {
            onRightImg();
        }

    }

    /**
     * 右边图片点击事件
     */
    protected void onRightImg() {


    }

    /**
     * 中间控件点击事件
     */
    protected void onMiddle() {

    }

    /**
     * 左边点击事件
     */
    protected void onBcak() {
        finish();
    }


    /**
     * 设置右边的文字
     *
     * @param resId
     */
    public void setRightText(int resId) {
        mRightText.setVisibility(View.VISIBLE);
        mRightImg.setVisibility(View.GONE);
        mRightText.setText(getResources().getString(resId));

    }


    /**
     * 设置右边的文字颜色
     *
     * @param resId
     */
    public void setRightTextColor(int resId) {
        mRightText.setTextColor(getResources().getColor(resId));
    }


    public void setNormalBar() {
        setVisibility(mBtnBack, View.VISIBLE);
        if (null != mSearchEditext) {
            mMiddleContainer.removeView(mSearchEditext);
        }

        setVisibility(mTitleTx, View.VISIBLE);
        setVisibility(mRightText, View.GONE);
        setVisibility(mRightImg, View.VISIBLE);
        setVisibility(mMiddleContainer, View.GONE);
        setRightImg(R.drawable.search_select);

    }


    public void setTextNormalBar() {
        setVisibility(mBtnBack, View.VISIBLE);
        if (null != mSearchEditext) {
            mMiddleContainer.removeView(mSearchEditext);
        }

        setVisibility(mTitleTx, View.VISIBLE);


        setVisibility(mRightText, View.GONE);
        setVisibility(mMiddleContainer, View.GONE);
        setVisibility(mRightImg, View.GONE);

    }

    /**
     * 设置标题
     *
     * @param resId
     */
    public void setTitle(int resId) {
        mTitleTx.setText(getResources().getString(resId));
    }


    /**
     * 设置标题
     *
     * @param resId
     */
    public void setTitle(String resId) {
        if (!TextUtils.isDigitsOnly(resId)) {

            mTitleTx.setText(resId);
        }
    }


    /**
     * 设置标题
     *
     * @param resId
     */
    public void setTextColor(int resId) {

        mTitleTx.setTextColor(resId);
    }


    /**
     * 设置右边的图片
     *
     * @param resId
     */
    public void setRightImg(int resId) {
        mRightText.setVisibility(View.GONE);
        mRightImg.setVisibility(View.VISIBLE);
        mRightImg.setImageDrawable(getResources().getDrawable(resId));
    }

    /**
     * 搜索逻辑处理
     */
    public void addSearchView(String hitString) {
        mSearchEditext = (EditText) View.inflate(this, R.layout.search_view, null);
        mSearchEditext.setOnTouchListener(onTouchListener);
        drawable = getResources().getDrawable(R.drawable.search_normal);
        closeDrawable = getResources().getDrawable(R.drawable.editext_close);
        drawable.setBounds(0, 0, (int) getResources().getDimension(R.dimen.dp_12), (int) getResources().getDimension(R.dimen.dp_12));
        closeDrawable.setBounds(0, 0, (int) getResources().getDimension(R.dimen.dp_16), (int) getResources().getDimension(R.dimen.dp_16));
        mSearchEditext.setCompoundDrawables(drawable, null, null, null);
        setEditTextInhibitInputSpace(mSearchEditext);

        if (!TextUtils.isEmpty(hitString)) {
            mSearchEditext.setHint(hitString);
            mSearchEditext.setHintTextColor(getResources().getColor(R.color.color_A9AAB3));
        }
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        layoutParams.height = (int) getResources().getDimension(R.dimen.dp_26);
        layoutParams.setMargins((int) getResources().getDimension(R.dimen.dp_15), 0, 0, 0);
        mSearchEditext.setLayoutParams(layoutParams);
        addMinddleView(mSearchEditext);
        setVisibility(mBtnBack, View.GONE);
        setRightText(R.string.cancel);
        setVisibility(mRightText, View.VISIBLE);
        setVisibility(mRightImg, View.GONE);
        setVisibility(mTitleTx, View.GONE);
        setVisibility(mMiddleContainer, View.VISIBLE);

        mSearchEditext.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {


            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                if (!TextUtils.isEmpty(charSequence)) {
                    if (null != mSearchEditext) {
                        mSearchEditext.setCompoundDrawables(drawable, null, closeDrawable, null);
                    }

                } else {
                    mSearchEditext.setCompoundDrawables(drawable, null, null, null);
                }

                ToolbarBaseActivity.this.onTextChanged(charSequence, i, i1, i2);

            }

            @Override
            public void afterTextChanged(Editable editable) {


            }
        });

    }

    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }


    View.OnTouchListener onTouchListener = new View.OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_UP) {
                if (null != mSearchEditext) {
                    if (mSearchEditext.getCompoundDrawables()[2] != null) {
                        boolean touchable = event.getX() > (mSearchEditext.getWidth() - mSearchEditext.getTotalPaddingRight())
                                && (event.getX() < ((mSearchEditext.getWidth() - mSearchEditext.getPaddingRight())));

                        if (touchable) {
                            mSearchEditext.post(new Runnable() {
                                @Override
                                public void run() {
//                                    mSearchEditext.setCompoundDrawables(drawable, null, null, null);
                                    mSearchEditext.setText("");
                                }
                            });
                        }
                    }
                }

            }


            return false;
        }
    };

    /**
     * @param editText 不能输入空值
     */
    public void setEditTextInhibitInputSpace(EditText editText) {
        InputFilter filter = new InputFilter() {
            @Override
            public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
                if (source.equals(" ")) {
                    return "";
                }
                return null;
            }
        };
        editText.setFilters(new InputFilter[]{filter});
    }

    /**
     * EditText获取焦点并显示软键盘
     */
    public void showSoftInputFromWindow(Activity activity, EditText editText) {
        editText.setFocusable(true);
        editText.setFocusableInTouchMode(true);
        editText.requestFocus();
        activity.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
    }
}
